package com.ego.ware.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.ego.common.exception.GlobalException;
import com.ego.common.vo.PageVo;
import com.ego.ware.vo.PurchaseFinishedVo;
import com.ego.ware.vo.PurchaseMergeVo;
import com.ego.ware.vo.PurchaseSearchVo;
import com.ego.ware.vo.PurchaseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.ware.po.Purchase;
import com.ego.ware.service.PurchaseService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @program: ego
 * @description 采购信息控制器
 * 流程：先创建采购需求，之后将采购需求合并至采购单分配给具体的采购人员，采购人员领取采购单后完成采购。
 * @author KamTang
 * @date 2022-11-13 13:30:29
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {

    @Autowired
    private PurchaseService purchaseService;

    /**
     * 完成采购
     */
    @PostMapping("/finished")
    public Result finished(@Valid @RequestBody PurchaseFinishedVo finished) {
        purchaseService.finished(finished);
        return Result.ok();
    }

    /**
     * 展示当前管理员涉及和可能涉及的采购单
     */
    @GetMapping("/list")
    public Result list(@RequestParam("adminId") Long adminId) {
        List<PurchaseVo> purchases;
        try {
            purchases = purchaseService.list(adminId);
        } catch (GlobalException e) {
            return Result.error(e.getCode(), e.getMsg());
        }
        return Result.ok().put("data", purchases);
    }

    /**
     * 采购员领取采购单
     */
    @PostMapping("/receive")
    public Result receive(@Valid @RequestBody List<Long> ids) {
        boolean flag = purchaseService.receive(ids);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    /**
     * 展示没有被接收的采购单
     */
    @GetMapping("/unreceived")
    public Result unreceived() {
        List<Purchase> purchases = purchaseService.unreceived();
        return Result.ok().put("data", purchases);
    }

    /**
     * 搜索
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody PurchaseSearchVo search) {
        PageVo<Purchase> page = purchaseService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 信息
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id){
		Purchase purchase = purchaseService.getById(id);
        return Result.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody Purchase purchase){
        purchase.setCreateTime(new Date());
		purchaseService.save(purchase);
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody Purchase purchase){
		purchaseService.updateById(purchase);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
